<template>
  <div>
    <el-card
      class="box-card"
      shadow="never"
      v-permission="21"
      :body-style="{ height: '0px', padding: '0px 0px' }"
    >
      <div slot="header" class="clearfix">
        <el-button @click="onAdd">添加</el-button>
      </div>
    </el-card>
    <div class="resource-body">
      <el-table
        :data="list"
        style="width: 100%; margin-bottom: 20px"
        v-loading="isLoading"
        border
      >
        <el-table-column type="index" label="编号" align="center" width="100">
        </el-table-column>
        <el-table-column prop="name" align="center" label="名称">
        </el-table-column>
        <el-table-column prop="createdTime" align="center" label="创建时间">
        </el-table-column>
        <el-table-column prop="sort" align="center" label="排序">
        </el-table-column>
        <el-table-column width="180" align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleEdit(scope.row)" v-permission="22"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      v-if="showDialog"
      :title="!isEdit ? '添加分类' : '编辑分类'"
      :visible.sync="showDialog"
      width="40%"
    >
      <category-form
        :isEdit="isEdit"
        :category="current"
        @on-cancel="showDialog = false"
        @on-submit="onSubmit"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getResourceCategories, deleteCategory } from '@/services/resource-category'
import CategoryForm from './form.vue'
import { dateFormat } from '@/utils/date'
export default Vue.extend({
  name: 'CategoryList',

  components: { CategoryForm },

  data () {
    return {
      list: [],
      isLoading: false,
      showDialog: false,
      isEdit: false,
      current: {} as FromServer.ResourceCategoryItem
    }
  },

  created () {
    this.loadCategories()
  },

  methods: {
    async loadCategories () {
      const { data } = await getResourceCategories()
      this.list = data.data.map((item: FromServer.ResourceCategoryItem) => {
        return {
          ...item,
          createdTime: dateFormat(item.createdTime)
        }
      })
    },
    onAdd () {
      this.isEdit = false
      this.showDialog = true
    },
    handleEdit (item: FromServer.ResourceCategoryItem) {
      this.isEdit = true
      this.current = item
      this.showDialog = true
    },
    handleDelete (item: FromServer.ResourceCategoryItem) {
      this.$confirm('是否要删除该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await deleteCategory(item.id)
          this.loadCategories()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    onSubmit () {
      this.showDialog = false
      this.loadCategories()
    }
  }
})
</script>

<style lang="scss" scoped>
.resource-body {
  .el-table {
    background-color: #fff;
  }
}
</style>
